<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        async function getPromise() {
            if (Math.random() < 0.5) {
                return 1;
            } else {
                throw 2;
            }
        }

        // console.log(getPromise());


        // async function test() {
        //     const result = await getPromise();  // 如果getPromise()返回的promise是rejected状态，则后面的异步代码不会执行
        //     console.log("正常状态", result)
        // }



        async function test() {
            try {
                const result = await getPromise();
                console.log("正常状态", result);   // 若await后面的promise是rejected状态，这句话不会执行
            } catch (err) {
                console.log("错误状态", err);
            }
        }

        test();
    </script>
</body>

</html>